<?php
// Lay cac thong so cau hinh chung
include "db_const.php";// Lay cac hang so dung chung
include "app_local.php";
include "isa-lib/isa-function/isa_public_function.php";
header('Content-Type: text/xml');
header("Cache-Control: no-cache, must-revalidate");
header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
function dbconnect() {
	 global $_ISA_SERVER_NAME ;
	 global $_ISA_DB_USER;
	 global $_ISA_DB_PASSWORD;
	 global $_ISA_DB_NAME;
	 $conn=@mssql_connect($_ISA_SERVER_NAME,$_ISA_DB_USER ,$_ISA_DB_PASSWORD);
	 mssql_select_db($_ISA_DB_NAME);
	 return $conn;
}
function get_all_recordtype($v_staff_id,$conn)
{
	$type = $_GET['type'];
	$v_is_granted_view_record = $_GET['is_granted'];
	switch($type)
	{
			case "TIEP_NHAN";
				$v_receiver = 1;
				$v_handle = -1;
				$v_handle_leader = -1;
				$v_approve_leader = -1;
				$v_is_view_on_net = -1;
				$v_is_register_on_net = -1;
				break;
			case "THU_LY";
				
				$v_receiver = -1;
				$v_handle = 1;			
				$v_handle_leader = -1;
				$v_approve_leader = -1;
				$v_is_view_on_net = -1;
				$v_is_register_on_net = -1;
				break;
			case "PHE_DUYET";
				$v_receiver = -1;
				$v_handle = -1;
				$v_handle_leader = 0;
				$v_approve_leader = 0;
				$v_is_view_on_net = -1;
				$v_is_register_on_net = -1;
				break;
			case "APPROVING_NET";
				$v_receiver = 1;
				$v_handle = -1;
				$v_handle_leader = -1;
				$v_approve_leader = -1;
				$v_is_view_on_net = -1;
				$v_is_register_on_net = 1;
				break;
			default:
				$v_receiver = -1;
				$v_handle = -1;
				$v_handle_leader = -1;
				$v_approve_leader = -1;
				$v_is_view_on_net = -1;
				$v_is_register_on_net = -1;
				break;
	}
	if ($v_is_granted_view_record)
	{
		$v_receiver = -1;
		$v_handle = -1;
		$v_handle_leader = -1;
		$v_approve_leader = -1;
		$v_is_view_on_net = -1;
		$v_is_register_on_net = -1;
	}
	$sql = "Exec ONEGATE_RecordtypeGetAllByStaff";
	$sql = $sql . " " . $v_staff_id;
	$sql = $sql . "," . $v_receiver;
	$sql = $sql . "," . $v_handle;
	$sql = $sql . "," . $v_handle_leader;
	$sql = $sql . "," . $v_approve_leader;
	$sql = $sql . "," . $v_is_view_on_net;
	$sql = $sql . "," . $v_is_register_on_net;		
	$result = mssql_query($sql,$conn);		
	return $result;
} 
function get_all_record($type,$conn)
{
	$v_staff_id ='';
	$v_recordtype_filter = '';
    $v_receive_date_filter = '';
    $v_status_list = '';
    $v_role = '';
	$v_status_of_handover = '';
	$v_status_of_assigning = '';
	$v_status_of_handle_approve = '';
	$v_status_of_chaiman_approve =  '';
	$v_order_clause = '';
	$v_detail_status_compare = '';
	$v_status_compare = '';
	if($type =='tn')
	{
		 $v_staff_id = $_GET['staff_id_tn'];
		 $v_recordtype_filter = $_GET['recordtype_filter_tn'];
		 $v_receive_date_filter = $_GET['receive_date_filter_tn'];
		 $v_status_list = $_GET['status_list_tn'];
		 $v_role = $_GET['role_tn'];
		 $v_status_of_handover = $_GET['status_of_handover_tn'];
		 $v_status_of_assigning = $_GET['status_of_assigning_tn'];
		 $v_status_of_handle_approve = $_GET['status_of_handle_approve_tn'];
		 $v_status_of_chaiman_approve =   $_GET['status_of_handle_approve_tn'];
		 $v_order_clause = $_GET['order_clause_tn'];
		 $v_detail_status_compare = $_GET['detail_status_compare_tn'];
		 $v_status_compare = $_GET['status_compare_tn'];
	}
	else if($type =='bs')
	{
		 $v_staff_id = $_GET['staff_id_tn'];
		 $v_recordtype_filter = $_GET['recordtype_filter_tn'];
		 $v_receive_date_filter = $_GET['receive_date_filter_bs'];
		 $v_status_list = $_GET['status_list_bs'];
		 $v_role = $_GET['role_bs'];
		 $v_status_of_handover = $_GET['status_of_handover_bs'];
		 $v_status_of_assigning = $_GET['status_of_assigning_bs'];
		 $v_status_of_handle_approve = $_GET['status_of_handle_approve_bs'];
		 $v_status_of_chaiman_approve =   $_GET['status_of_handle_approve_bs'];
		 $v_order_clause = $_GET['order_clause_bs'];
		 $v_detail_status_compare = $_GET['detail_status_compare_bs'];
		 $v_status_compare = $_GET['status_compare_bs'];
	}
	else if($type =='dgq')
	{
		 $v_staff_id = $_GET['staff_id_tn'];
		 $v_recordtype_filter = $_GET['recordtype_filter_tn'];
		$v_receive_date_filter = $_GET['receive_date_filter_dgq'];
		$v_status_list = $_GET['status_list_dgq'];
		$v_role = $_GET['role_dgq'];
		$v_status_of_handover = $_GET['status_of_handover_dgq'];
		$v_status_of_assigning = $_GET['status_of_assigning_dgq'];
		$v_status_of_handle_approve = $_GET['status_of_handle_approve_dgq'];
		$v_status_of_chaiman_approve =   $_GET['status_of_handle_approve_dgq'];
		$v_order_clause = $_GET['order_clause_dgq'];
		$v_detail_status_compare = $_GET['detail_status_compare_dgq'];
		$v_status_compare = $_GET['status_compare_dgq'];
	}
	/*----------------------thu ly ho so--------------------------------------------------------*/
	else if($type=='dangThuLy')
	{
		$v_staff_id = $_GET['staff_id'];
		 $v_recordtype_filter = $_GET['recordtype_filter'];		
		 $v_receive_date_filter = $_GET['receive_date_filter'];
		 $v_status_list = $_GET['status_list'];
		 $v_role = $_GET['role'];
		 $v_status_of_handover = $_GET['status_of_handover'];
		 $v_status_of_assigning = $_GET['status_of_assigning'];
		 $v_status_of_handle_approve = $_GET['status_of_handle_approve'];		
		 $v_status_of_chaiman_approve =   $_GET['status_of_handle_approve'];
		 $v_order_clause = $_GET['order_clause'];
		 $v_detail_status_compare = $_GET['detail_status_compare'];
		 $v_status_compare = $_GET['status_compare'];
	}
	else if($type =='cbs')
	{
		$v_staff_id = $_GET['staff_id'];
		 $v_recordtype_filter = $_GET['recordtype_filter'];
		 $v_receive_date_filter = $_GET['receive_date_filter_cbs'];
		 $v_status_list = $_GET['status_list_cbs'];
		 $v_role = $_GET['role_cbs'];
		 $v_status_of_handover = $_GET['status_of_handover_cbs'];
		 $v_status_of_assigning = $_GET['status_of_assigning_cbs'];
		 $v_status_of_handle_approve = $_GET['status_of_handle_approve_cbs'];
		 $v_status_of_chaiman_approve =   $_GET['status_of_handle_approve_cbs'];
		 $v_order_clause = $_GET['order_clause_cbs'];
		 $v_detail_status_compare = $_GET['detail_status_compare_cbs'];
		 $v_status_compare = $_GET['status_compare_cbs'];
	}
	else if($type =='dtk')
	{
		$v_staff_id = $_GET['staff_id'];
		 $v_recordtype_filter = $_GET['recordtype_filter'];
		 $v_receive_date_filter = $_GET['receive_date_filter_dtk'];
		 $v_status_list = $_GET['status_list_dtk'];
		 $v_role = $_GET['role_dtk'];
		 $v_status_of_handover = $_GET['status_of_handover_dtk'];
		 $v_status_of_assigning = $_GET['status_of_assigning_dtk'];
		 $v_status_of_handle_approve = $_GET['status_of_handle_approve_dtk'];
		 $v_status_of_chaiman_approve =   $_GET['status_of_handle_approve_dtk'];
		 $v_order_clause = $_GET['order_clause_dtk'];
		 $v_detail_status_compare = $_GET['detail_status_compare_dtk'];
		 $v_status_compare = $_GET['status_compare_dtk'];
	}

	else if($type =='dgq')
	{
		$v_staff_id = $_GET['staff_id'];
		 $v_recordtype_filter = $_GET['recordtype_filter'];
		 $v_receive_date_filter = $_GET['receive_date_filter_dgq'];
		 $v_status_list = $_GET['status_list_dgq'];
		 $v_role = $_GET['role_dgq'];
		 $v_status_of_handover = $_GET['status_of_handover_dgq'];
		 $v_status_of_assigning = $_GET['status_of_assigning_dgq'];
		 $v_status_of_handle_approve = $_GET['status_of_handle_approve_dgq'];
		 $v_status_of_chaiman_approve =   $_GET['status_of_handle_approve_dgq'];
		 $v_order_clause = $_GET['order_clause_dgq'];
		 $v_detail_status_compare = $_GET['detail_status_compare_dgq'];
		 $v_status_compare = $_GET['status_compare_dgq'];
	}
	else if($type =='cc')
	{
		$v_staff_id = $_GET['staff_id'];
		 $v_recordtype_filter = $_GET['recordtype_filter'];
		 $v_receive_date_filter = $_GET['receive_date_filter_cct'];
		 $v_status_list = $_GET['status_list_cc'];
		 $v_role = $_GET['role_cc'];
		 $v_status_of_handover = $_GET['status_of_handover_cc'];
		 $v_status_of_assigning = $_GET['status_of_assigning_cc'];
		 $v_status_of_handle_approve = $_GET['status_of_handle_approve_cct'];
		 $v_status_of_chaiman_approve =   $_GET['status_of_handle_approve_cct'];
		 $v_order_clause = $_GET['order_clause_cc'];
		 $v_detail_status_compare = $_GET['detail_status_compare_cc'];
		 $v_status_compare = $_GET['status_compare_cc'];
	}
	/*--------------------phe duyet ho so --------------------------------------------------*/
	else if($type =='pc')
	{
		$v_staff_id = $_GET['staff_id_pc'];
		 $v_recordtype_filter = $_GET['recordtype_filter_pc'];
		 $v_receive_date_filter = $_GET['receive_date_filter_pc'];
		 $v_status_list = $_GET['status_list_pc'];
		 $v_role = $_GET['role_pc'];
		 $v_status_of_handover = $_GET['status_of_handover_pc'];
		 $v_status_of_assigning = $_GET['status_of_assigning_pc'];
		 $v_status_of_handle_approve = $_GET['status_of_handle_approve_pc'];
		 $v_status_of_chaiman_approve =   $_GET['status_of_handle_approve_pc'];
		 $v_order_clause = $_GET['order_clause_pc'];
		 $v_detail_status_compare = $_GET['detail_status_compare_pc'];
		 $v_status_compare = $_GET['status_compare_pc'];
	}
	else if($type =='td')
	{
		$v_staff_id = $_GET['staff_id_pc'];
		 $v_recordtype_filter = $_GET['recordtype_filter_pc'];
		 $v_receive_date_filter = $_GET['receive_date_filter_td'];
		 $v_status_list = $_GET['status_list_td'];
		 $v_role = $_GET['role_td'];
		 $v_status_of_handover = $_GET['status_of_handover_td'];
		 $v_status_of_assigning = $_GET['status_of_assigning_td'];
		 $v_status_of_handle_approve = $_GET['status_of_handle_approve_td'];
		 $v_status_of_chaiman_approve =   $_GET['status_of_handle_approve_td'];
		 $v_order_clause = $_GET['order_clause_td'];
		 $v_detail_status_compare = $_GET['detail_status_compare_td'];
		 $v_status_compare = $_GET['status_compare_td'];
	}
	else if($type =='cd')
	{
		$v_staff_id = $_GET['staff_id_pc'];
		 $v_recordtype_filter = $_GET['recordtype_filter_pc'];
		 $v_receive_date_filter = $_GET['receive_date_filter_cd'];
		 $v_status_list = $_GET['status_list_cd'];
		 $v_role = $_GET['role_cd'];
		 $v_status_of_handover = $_GET['status_of_handover_cd'];
		 $v_status_of_assigning = $_GET['status_of_assigning_cd'];
		 $v_status_of_handle_approve = $_GET['status_of_handle_approve_cd'];
		 $v_status_of_chaiman_approve =   $_GET['status_of_handle_approve_cd'];
		 $v_order_clause = $_GET['order_clause_cd'];
		 $v_detail_status_compare = $_GET['detail_status_compare_cd'];
		 $v_status_compare = $_GET['status_compare_cd'];
	}
	else if($type =='dd')
	{
		$v_staff_id = $_GET['staff_id_pc'];
		 $v_recordtype_filter = $_GET['recordtype_filter_pc'];
		 $v_receive_date_filter = $_GET['receive_date_filter_dd'];
		 $v_status_list = $_GET['status_list_dd'];
		 $v_role = $_GET['role_dd'];
		 $v_status_of_handover = $_GET['status_of_handover_dd'];
		 $v_status_of_assigning = $_GET['status_of_assigning_dd'];
		 $v_status_of_handle_approve = $_GET['status_of_handle_approve_dd'];
		 $v_status_of_chaiman_approve =   $_GET['status_of_handle_approve_dd'];
		 $v_order_clause = $_GET['order_clause_dd'];
		 $v_detail_status_compare = $_GET['detail_status_compare_dd'];
		 $v_status_compare = $_GET['status_compare_dd'];
	}
	else if($type =='th')
	{
		$v_staff_id = $_GET['staff_id_pc'];
		 $v_recordtype_filter = $_GET['recordtype_filter_pc'];
		 $v_receive_date_filter = $_GET['receive_date_filter_th'];
		 $v_status_list = $_GET['status_list_th'];
		 $v_role = $_GET['role_th'];
		 $v_status_of_handover = $_GET['status_of_handover_th'];
		 $v_status_of_assigning = $_GET['status_of_assigning_th'];
		 $v_status_of_handle_approve = $_GET['status_of_handle_approve_th'];
		 $v_status_of_chaiman_approve =   $_GET['status_of_handle_approve_th'];
		 $v_order_clause = $_GET['order_clause_th'];
		 $v_detail_status_compare = $_GET['detail_status_compare_th'];
		 $v_status_compare = $_GET['status_compare_th'];
	}
	//echo $v_recordtype_filter;
	if($v_recordtype_filter == 'All')
	{
			$v_total_record = 0;
			$result_recordtype = get_all_recordtype($v_staff_id,$conn);
			
			while($arr_recordtype = mssql_fetch_array($result_recordtype, MSSQL_ASSOC))
			{			//print_r ($arr_recordtype);
//						echo $v_recordtype_filte;
						$v_recordtype_filter = $arr_recordtype['PK_RECORDTYPE'];
						$sql = " exec Onegate_RecordGetAll_Tuyenntt";
						$sql = $sql . " " . $v_staff_id ;
						$sql = $sql . ",'" . $v_recordtype_filter . "'" ;
						$sql = $sql . ",'" .$v_receive_date_filter. "'" ;
						$sql = $sql . ",'" . $v_status_list."'" ;
						$sql = $sql . ",'" . $v_role."'" ;
						$sql = $sql . "," . $v_status_of_handover;
						$sql = $sql . "," . $v_status_of_assigning;
						$sql = $sql . "," . $v_status_of_handle_approve;
						$sql = $sql . "," . $v_status_of_chaiman_approve;
						$sql = $sql . ",'" . $v_order_clause."'" ;
						$sql = $sql . ",'". $v_detail_status_compare."'" ;	
						$sql = $sql . "," . $v_status_compare;							
						$result = mssql_query($sql,$conn);
						$row =@mssql_fetch_array($result);
						$v_total_record = $v_total_record + $row['TOTAL_RECORD'];
						//_write_log_file('tuyenntt.txt',$sql);
				
			}		
	}
	else
	{
		$sql = " exec Onegate_RecordGetAll_Tuyenntt";
		$sql = $sql . " " . $v_staff_id ;
		$sql = $sql . ",'" . $v_recordtype_filter . "'" ;
		$sql = $sql . ",'" .$v_receive_date_filter. "'" ;
		$sql = $sql . ",'" . $v_status_list."'" ;
		$sql = $sql . ",'" . $v_role."'" ;
		$sql = $sql . "," . $v_status_of_handover;
		$sql = $sql . "," . $v_status_of_assigning;
		$sql = $sql . "," . $v_status_of_handle_approve;
	//echo $v_status_of_chaiman_approve;
		$sql = $sql . "," . $v_status_of_chaiman_approve;
		$sql = $sql . ",'" . $v_order_clause."'" ;
		$sql = $sql . ",'". $v_detail_status_compare."'" ;	
		$sql = $sql . "," . $v_status_compare;	
		//_write_log_file('tuyenntt.txt',$sql);
		$result = mssql_query($sql,$conn);
		$row =@mssql_fetch_array($result);
		$v_total_record = $row['TOTAL_RECORD'];			
	}	
	return $v_total_record;	
}
$conn = dbconnect();
$type = $_GET['type'];
$v_size_tiep_nhan = 0;
if($type=='TIEP_NHAN')
{
	$v_size_tiepnhan = get_all_record('tn',$conn)	;
	if($v_size_tiepnhan == null)
		$v_size_tiepnhan = 0;
	$v_size_bosung = get_all_record('bs',$conn)	;
	if($v_size_bosung == null)
		$v_size_bosung = 0;
	$v_size_danggiaiquyet = get_all_record('dgq',$conn)	;
	if($v_size_danggiaiquyet == null)
		$v_size_danggiaiquyet = 0;
 	echo '<?xml version="1.0" encoding="ISO-8859-1"?>
	<root>';
	/*-------------------tiep nhan va tra ket qua -----------------------------*/
		echo '<v_size_tiepnhan>'.$v_size_tiepnhan.'</v_size_tiepnhan>';	
		echo '<v_size_bosung>'.$v_size_bosung.'</v_size_bosung>';	
		echo '<v_size_danggiaiquyet>'.$v_size_danggiaiquyet.'</v_size_danggiaiquyet>';	
   /*--------------------------ket thuc giai quyet ho so -----------------------*/		
	echo '</root>';
}
else if($type =='THU_LY'){	
	$v_size_dangThuLy = get_all_record('dangThuLy',$conn)	;
	if($v_size_dangThuLy== null){
		$v_size_dangThuLy = 0;
	}
	$v_size_choBoSung = get_all_record('cbs',$conn)	;
	if($v_size_choBoSung== null ){
		$v_size_choBoSung =0;
	}
	$v_size_dangTrinhKy = get_all_record('dtk',$conn)	;
	if($v_size_dangTrinhKy== null){
		$v_size_dangTrinhKy =0 ;
	}
	$v_size_daGiaiQuyet = get_all_record('dgq',$conn)	;
	if($v_size_daGiaiQuyet== null){
		$v_size_daGiaiQuyet =0;
	}
	$v_size_chuyenCap = get_all_record('cc',$conn)	;
	if($v_size_chuyenCap== null){
		$v_size_chuyenCap =0;
	}
	echo '<?xml version="1.0" encoding="ISO-8859-1"?>
	<root>';
		/*------------------bat dau thu ly ho so ----------------------------------*/
		echo '<v_size_dangThuLy>'.$v_size_dangThuLy.'</v_size_dangThuLy>';
		echo '<v_size_choBoSung>'.$v_size_choBoSung.'</v_size_choBoSung>';	
		echo '<v_size_dangTrinhKy>'.$v_size_dangTrinhKy.'</v_size_dangTrinhKy>';
		echo '<v_size_daGiaiQuyet>'.$v_size_daGiaiQuyet.'</v_size_daGiaiQuyet>';	
		echo '<v_size_chuyenCap>'.$v_size_chuyenCap.'</v_size_chuyenCap>';
	/*-----------------ket thuc thu ly ho so ----------------------------------*/
	echo '</root>';
}
else if($type == 'PHE_DUYET'){
	$v_size_phanCongThuLy = get_all_record('pc',$conn)	;
	if($v_size_phanCongThuLy ==null){
		$v_size_phanCongThuLy = 0;
	}

	$v_size_thayDoiThuLy = get_all_record('td',$conn)	;
	if($v_size_thayDoiThuLy== null){
		$v_size_thayDoiThuLy =0;
	}
	$v_size_choDuyet = get_all_record('cd',$conn)	;
	if($v_size_choDuyet == null){
		$v_size_choDuyet =0;
	}
	$v_size_daDuyet = get_all_record('dd',$conn)	;
	if($v_size_daDuyet == null){
		$v_size_daDuyet =0;
	}
	$v_size_tinhHinhGiaiQuyet = get_all_record('th',$conn)	;
	if($v_size_tinhHinhGiaiQuyet == null){
		$v_size_tinhHinhGiaiQuyet = 0;
	}
	echo '<?xml version="1.0" encoding="ISO-8859-1"?>
	<root>';
		/*--------------------------bat dau giai quyet ho so ---------------------------*/

		echo '<v_size_phanCongThuLy>'.$v_size_phanCongThuLy.'</v_size_phanCongThuLy>';	
		echo '<v_size_thayDoiThuLy>'.$v_size_thayDoiThuLy.'</v_size_thayDoiThuLy>';	
		echo '<v_size_choDuyet>'.$v_size_choDuyet.'</v_size_choDuyet>';	
		echo '<v_size_daDuyet>'.$v_size_daDuyet.'</v_size_daDuyet>';	
		echo '<v_size_tinhHinhGiaiQuyet>'.$v_size_tinhHinhGiaiQuyet.'</v_size_tinhHinhGiaiQuyet>';
		/*--------------------------KET THUC PHE DUYET HO SO----------------------------*/
	echo '</root>';
}
mssql_close($conn);
?>